/*
 * @Author: your name
 * @Date: 2020-11-16 22:43:05
 * @LastEditTime: 2020-11-16 22:51:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \movie\controllers\admin\hotController.js
 */
// const path = require('path')
// const fs = require('fs')
// const { join } = require('path')
// const hotModel = require('../../db/models/hotModel')

const path = require('path')
const fs = require('fs')
const { join } = require('path')
const hotModel = require('../../db/models/hotModel')
//后台首页控制器
module.exports = {
  //添加显示
  async index( req, res ) {
   let data = await hotModel.all()

   res.render( 'admin/hot/index', {data} )
  },
  create ( req, res ) { 
    res.render( 'admin/hot/create' );
  },
  async store ( req, res ) {
    // const file = req.file;
    //表单数据
    const postData = req.body;
    // if ( !file ) {
    //   postData.pic = '/uploads/默认图片.jpg';
    // } else { 
    //   //让上传的图片有扩展名
    //   //file对象中的originalname属性得到扩展名
    //   let ext = path.extname( file.originalname );

    //   let filename = file.filename + ext;
    //   fs.renameSync( file.path, join( __dirname, '../../static/uploads' ) + filename );

    //   //保存网络地址到数据库中
    //   postData.pic = '/uploads/' + filename;
    // }



    //上传数据到数据库
    // console.log( postData );
    

    //添加到数据表中
    await hotModel.addHot( postData );

    res.redirect( '/admin/hot/index' );
  }
}